<?php

/*
 * 多线程采集类
 *
*/
set_time_limit(0);
ini_set('memory_limit', '128M');
class MyCurl_muilt {

	public function remote($urls) {

		if (!is_array($urls) or count($urls) == 0) {
			return false;
		}

		$curl = $text = array ();
		$handle = curl_multi_init();
		foreach ($urls as $k => $v) {
			//      $nurl[$k]= preg_replace('~([^:\/\.]+)~ei', "rawurlencode('\\1')", $v);
			//        $nurl[$k]= preg_replace_callback("/^[\x{4e00}-\x{9fa5}]/u ","urlbm",$v);
			$nurl[$k] = $v;
			$curl[$k] = curl_init($nurl[$k]);
			curl_setopt($curl[$k], CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($curl[$k], CURLOPT_HEADER, 0);
			curl_multi_add_handle($handle, $curl[$k]);
		}

		$active = null;
		do {
			$mrc = curl_multi_exec($handle, $active);
		} while ($mrc == CURLM_CALL_MULTI_PERFORM);

		while ($active && $mrc == CURLM_OK) {
			if (curl_multi_select($handle) != -1) {
				do {
					$mrc = curl_multi_exec($handle, $active);
				} while ($mrc == CURLM_CALL_MULTI_PERFORM);
			}
		}

		foreach ($curl as $k => $v) {
			if (curl_error($curl[$k]) == "") {
				$text[$k] = (string) curl_multi_getcontent($curl[$k]);
			}
			curl_multi_remove_handle($handle, $curl[$k]);
			curl_close($curl[$k]);
		}
		curl_multi_close($handle);
		return $text;
	}
}